package com.ruoyix.weblog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.weblog.domain.bo.BlogFamilyBo;
import com.ruoyix.weblog.domain.vo.BlogFamilyVo;
import com.ruoyix.weblog.domain.BlogFamily;
import com.ruoyix.weblog.mapper.BlogFamilyMapper;
import com.ruoyix.weblog.service.IBlogFamilyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 家庭信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class BlogFamilyServiceImpl implements IBlogFamilyService {

    private final BlogFamilyMapper baseMapper;

    /**
     * 查询家庭信息
     */
    @Override
    public BlogFamilyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询家庭信息列表
     */
    @Override
    public TableDataInfo<BlogFamilyVo> queryPageList(BlogFamilyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogFamily> lqw = buildQueryWrapper(bo);
        Page<BlogFamilyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询家庭信息列表
     */
    @Override
    public List<BlogFamilyVo> queryList(BlogFamilyBo bo) {
        LambdaQueryWrapper<BlogFamily> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogFamily> buildQueryWrapper(BlogFamilyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogFamily> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, BlogFamily::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getBgCover()), BlogFamily::getBgCover, bo.getBgCover());
        lqw.eq(StringUtils.isNotBlank(bo.getManCover()), BlogFamily::getManCover, bo.getManCover());
        lqw.eq(StringUtils.isNotBlank(bo.getWomanCover()), BlogFamily::getWomanCover, bo.getWomanCover());
        lqw.like(StringUtils.isNotBlank(bo.getManName()), BlogFamily::getManName, bo.getManName());
        lqw.like(StringUtils.isNotBlank(bo.getWomanName()), BlogFamily::getWomanName, bo.getWomanName());
        lqw.eq(StringUtils.isNotBlank(bo.getTiming()), BlogFamily::getTiming, bo.getTiming());
        lqw.eq(StringUtils.isNotBlank(bo.getCountdownTitle()), BlogFamily::getCountdownTitle, bo.getCountdownTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getCountdownTime()), BlogFamily::getCountdownTime, bo.getCountdownTime());
        lqw.eq(bo.getStatus() != null, BlogFamily::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFamilyInfo()), BlogFamily::getFamilyInfo, bo.getFamilyInfo());
        lqw.eq(bo.getLikeCount() != null, BlogFamily::getLikeCount, bo.getLikeCount());
        return lqw;
    }

    /**
     * 新增家庭信息
     */
    @Override
    public Boolean insertByBo(BlogFamilyBo bo) {
        BlogFamily add = BeanUtil.toBean(bo, BlogFamily.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改家庭信息
     */
    @Override
    public Boolean updateByBo(BlogFamilyBo bo) {
        BlogFamily update = BeanUtil.toBean(bo, BlogFamily.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogFamily entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除家庭信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
